package com.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dao.OrderSettingDao;
import com.pojo.OrderSetting;
import com.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void add(List<OrderSetting> list) {
        if (list != null && list.size() > 0){
            for (OrderSetting orderSetting : list) {
                //判断当前日期是否有木有存入
                Long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (count > 0){
                    //已经存入，更新为最新数据
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else{
                    //木有存入 ，进行存入操作
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    @Override      //调查预约设置
    public List<Map> getOrderSettingByMonth(String data) {
        String begin = data + "-1";
        String end = data + "-31";
        Map<String,String> map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        List<Map> mapList = new ArrayList<>();
        if (list !=null ){
            for (OrderSetting orderSetting : list) {
                Map<String,Object> objectMap = new HashMap<>();
                objectMap.put("date",orderSetting.getOrderDate().getDate());
                objectMap.put("number",orderSetting.getNumber());
                objectMap.put("reservations",orderSetting.getReservations());
                mapList.add(objectMap);
            }
        }
        return mapList;
    }

    @Override       //更改预约设置
    public void editNumberByDate(OrderSetting orderSetting) {
        Date orderDate = orderSetting.getOrderDate();
        //查询当前的年月
        Long count = orderSettingDao.findCountByOrderDate(orderDate);
        if (count > 0 ){
            //当前年月已经设置，那么进行更新设置
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else{
            //当前年月未曾设置，进行添加操作
            orderSettingDao.add(orderSetting);
        }
    }
}
